2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #ifndef __JUCER_PAINTELEMENTTEXT_JUCEHEADER__
27 #define __JUCER_PAINTELEMENTTEXT_JUCEHEADER__
29 #include "jucer_ColouredElement.h"
30 #include "../../properties/jucer_FontPropertyComponent.h"
31 #include "../../properties/jucer_JustificationProperty.h"
34 //==============================================================================
37 class PaintElementText
: public ColouredElement
40 //==============================================================================
41 PaintElementText (PaintRoutine
* owner
)
42 : ColouredElement (owner
, "Text", false, false),
43 text ("Your text goes here"),
45 typefaceName (FontPropertyComponent::defaultFont
),
46 justification (Justification::centred
)
48 fillType
.colour
= Colours::black
;
49 position
.rect
.setWidth (200);
50 position
.rect
.setHeight (30);
57 //==============================================================================
58 void draw (Graphics
& g
, const ComponentLayout
* layout
, const Rectangle
<int>& parentArea
)
60 fillType
.setFillType (g
, getDocument(), parentArea
);
62 font
= FontPropertyComponent::applyNameToFont (typefaceName
, font
);
65 const Rectangle
<int> r (position
.getRectangle (parentArea
, layout
));
66 g
.drawText (replaceStringTranslations (text
, owner
->getDocument()),
67 r
.getX(), r
.getY(), r
.getWidth(), r
.getHeight(),
71 void getEditableProperties (Array
<PropertyComponent
*>& properties
)
73 ColouredElement::getEditableProperties (properties
);
75 properties
.add (new TextProperty (this));
76 properties
.add (new FontNameProperty (this));
77 properties
.add (new FontStyleProperty (this));
78 properties
.add (new FontSizeProperty (this));
79 properties
.add (new TextJustificationProperty (this));
80 properties
.add (new TextToPathProperty (this));
83 void fillInGeneratedCode (GeneratedCode
& code
, String
& paintMethodCode
)
85 if (! fillType
.isInvisible())
89 fillType
.fillInGeneratedCode (code
, paintMethodCode
);
92 positionToCode (position
, code
.document
->getComponentLayout(), x
, y
, w
, h
);
95 << FontPropertyComponent::getCompleteFontCode (font
, typefaceName
)
97 << quotedString (text
)
99 << x
<< ", " << y
<< ", " << w
<< ", " << h
101 << justificationToCode (justification
)
104 paintMethodCode
+= r
;
108 static const char* getTagName() throw() { return "TEXT"; }
110 XmlElement
* createXml() const
112 XmlElement
* e
= new XmlElement (getTagName());
113 position
.applyToXml (*e
);
114 addColourAttributes (e
);
115 e
->setAttribute ("text", text
);
116 e
->setAttribute ("fontname", typefaceName
);
117 e
->setAttribute ("fontsize", roundToInt (font
.getHeight() * 100.0) / 100.0);
118 e
->setAttribute ("bold", font
.isBold());
119 e
->setAttribute ("italic", font
.isItalic());
120 e
->setAttribute ("justification", justification
.getFlags());
125 bool loadFromXml (const XmlElement
& xml
)
127 if (xml
.hasTagName (getTagName()))
129 position
.restoreFromXml (xml
, position
);
130 loadColourAttributes (xml
);
132 text
= xml
.getStringAttribute ("text", "Hello World");
133 typefaceName
= xml
.getStringAttribute ("fontname", FontPropertyComponent::defaultFont
);
134 font
.setHeight ((float) xml
.getDoubleAttribute ("fontsize", 15.0));
135 font
.setBold (xml
.getBoolAttribute ("bold", false));
136 font
.setItalic (xml
.getBoolAttribute ("italic", false));
137 justification
= Justification (xml
.getIntAttribute ("justification", Justification::centred
));
148 //==============================================================================
149 const String
& getText() const throw() { return text
; }
151 class SetTextAction
: public PaintElementUndoableAction
<PaintElementText
>
154 SetTextAction (PaintElementText
* const element
, const String
& newText_
)
155 : PaintElementUndoableAction
<PaintElementText
> (element
),
157 oldText (element
->getText())
164 getElement()->setText (newText
, false);
171 getElement()->setText (oldText
, false);
176 String newText
, oldText
;
179 void setText (const String
& t
, const bool undoable
)
185 perform (new SetTextAction (this, t
),
186 "Change text element text");
196 //==============================================================================
197 const Font
& getFont() const { return font
; }
199 class SetFontAction
: public PaintElementUndoableAction
<PaintElementText
>
202 SetFontAction (PaintElementText
* const element
, const Font
& newFont_
)
203 : PaintElementUndoableAction
<PaintElementText
> (element
),
205 oldFont (element
->getFont())
212 getElement()->setFont (newFont
, false);
219 getElement()->setFont (oldFont
, false);
224 Font newFont
, oldFont
;
227 void setFont (const Font
& newFont
, const bool undoable
)
233 perform (new SetFontAction (this, newFont
),
234 "Change text element font");
244 //==============================================================================
245 class SetTypefaceAction
: public PaintElementUndoableAction
<PaintElementText
>
248 SetTypefaceAction (PaintElementText
* const element
, const String
& newValue_
)
249 : PaintElementUndoableAction
<PaintElementText
> (element
),
250 newValue (newValue_
),
251 oldValue (element
->getTypefaceName())
258 getElement()->setTypefaceName (newValue
, false);
265 getElement()->setTypefaceName (oldValue
, false);
270 String newValue
, oldValue
;
273 void setTypefaceName (const String
& newFontName
, const bool undoable
)
277 perform (new SetTypefaceAction (this, newFontName
),
278 "Change text element typeface");
282 typefaceName
= newFontName
;
287 const String
getTypefaceName() const throw() { return typefaceName
; }
289 //==============================================================================
290 const Justification
& getJustification() const throw() { return justification
; }
292 class SetJustifyAction
: public PaintElementUndoableAction
<PaintElementText
>
295 SetJustifyAction (PaintElementText
* const element
, const Justification
& newValue_
)
296 : PaintElementUndoableAction
<PaintElementText
> (element
),
297 newValue (newValue_
),
298 oldValue (element
->getJustification())
305 getElement()->setJustification (newValue
, false);
312 getElement()->setJustification (oldValue
, false);
317 Justification newValue
, oldValue
;
320 void setJustification (const Justification
& j
, const bool undoable
)
322 if (justification
.getFlags() != j
.getFlags())
326 perform (new SetJustifyAction (this, j
),
327 "Change text element justification");
339 font
= FontPropertyComponent::applyNameToFont (typefaceName
, font
);
341 const Rectangle
<int> r (getCurrentAbsoluteBounds());
343 GlyphArrangement arr
;
344 arr
.addCurtailedLineOfText (font
, text
,
345 0.0f
, 0.0f
, (float) r
.getWidth(),
348 arr
.justifyGlyphs (0, arr
.getNumGlyphs(),
349 (float) r
.getX(), (float) r
.getY(),
350 (float) r
.getWidth(), (float) r
.getHeight(),
354 arr
.createPath (path
);
356 convertToNewPathElement (path
);
363 Justification justification
;
365 Array
<Justification
> justificationTypes
;
367 //==============================================================================
368 class TextProperty
: public TextPropertyComponent
,
369 public ChangeListener
372 TextProperty (PaintElementText
* const element_
)
373 : TextPropertyComponent ("text", 2048, false),
376 element
->getDocument()->addChangeListener (this);
381 element
->getDocument()->removeChangeListener (this);
384 void setText (const String
& newText
) { element
->setText (newText
, true); }
385 const String
getText() const { return element
->getText(); }
387 void changeListenerCallback (ChangeBroadcaster
*) { refresh(); }
390 PaintElementText
* const element
;
393 //==============================================================================
394 class FontNameProperty
: public FontPropertyComponent
,
395 public ChangeListener
398 FontNameProperty (PaintElementText
* const element_
)
399 : FontPropertyComponent ("font"),
402 element
->getDocument()->addChangeListener (this);
407 element
->getDocument()->removeChangeListener (this);
410 void setTypefaceName (const String
& newFontName
) { element
->setTypefaceName (newFontName
, true); }
411 const String
getTypefaceName() const { return element
->getTypefaceName(); }
413 void changeListenerCallback (ChangeBroadcaster
*) { refresh(); }
416 PaintElementText
* const element
;
419 //==============================================================================
420 class FontStyleProperty
: public ChoicePropertyComponent
,
421 public ChangeListener
424 FontStyleProperty (PaintElementText
* const element_
)
425 : ChoicePropertyComponent ("style"),
428 element
->getDocument()->addChangeListener (this);
430 choices
.add ("normal");
431 choices
.add ("bold");
432 choices
.add ("italic");
433 choices
.add ("bold + italic");
438 element
->getDocument()->removeChangeListener (this);
441 void setIndex (int newIndex
)
443 Font
f (element
->getFont());
445 f
.setBold (newIndex
== 1 || newIndex
== 3);
446 f
.setItalic (newIndex
== 2 || newIndex
== 3);
448 element
->setFont (f
, true);
453 if (element
->getFont().isBold() && element
->getFont().isItalic())
455 else if (element
->getFont().isBold())
457 else if (element
->getFont().isItalic())
463 void changeListenerCallback (ChangeBroadcaster
*) { refresh(); }
466 PaintElementText
* const element
;
469 //==============================================================================
470 class FontSizeProperty
: public SliderPropertyComponent
,
471 public ChangeListener
474 FontSizeProperty (PaintElementText
* const element_
)
475 : SliderPropertyComponent ("size", 1.0, 250.0, 0.1, 0.3),
478 element
->getDocument()->addChangeListener (this);
483 element
->getDocument()->removeChangeListener (this);
486 void setValue (double newValue
)
488 element
->getDocument()->getUndoManager().undoCurrentTransactionOnly();
490 Font
f (element
->getFont());
491 f
.setHeight ((float) newValue
);
493 element
->setFont (f
, true);
496 double getValue() const
498 return element
->getFont().getHeight();
501 void changeListenerCallback (ChangeBroadcaster
*) { refresh(); }
504 PaintElementText
* const element
;
507 //==============================================================================
508 class TextJustificationProperty
: public JustificationProperty
,
509 public ChangeListener
512 TextJustificationProperty (PaintElementText
* const element_
)
513 : JustificationProperty ("layout", false),
516 element
->getDocument()->addChangeListener (this);
519 ~TextJustificationProperty()
521 element
->getDocument()->removeChangeListener (this);
524 void setJustification (const Justification
& newJustification
)
526 element
->setJustification (newJustification
, true);
529 const Justification
getJustification() const
531 return element
->getJustification();
534 void changeListenerCallback (ChangeBroadcaster
*) { refresh(); }
537 PaintElementText
* const element
;
540 //==============================================================================
541 class TextToPathProperty
: public ButtonPropertyComponent
544 TextToPathProperty (PaintElementText
* const element_
)
545 : ButtonPropertyComponent ("path", false),
552 element
->convertToPath();
555 const String
getButtonText() const
557 return "convert text to a path";
561 PaintElementText
* const element
;
566 #endif // __JUCER_PAINTELEMENTTEXT_JUCEHEADER__